package com.ruoyi.wx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wx.domain.WxInfo;
import com.ruoyi.wx.service.IWxInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 租户余额Controller
 * 
 * @author wt
 * @date 2020-12-13
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wx/wxInfo" )
public class WxInfoController extends BaseController {

    private final IWxInfoService iWxInfoService;

    /**
     * 查询租户余额列表
     */
    @PreAuthorize("@ss.hasPermi('wx:wxInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxInfo wxInfo)
    {
        startPage();
        LambdaQueryWrapper<WxInfo> lqw = Wrappers.lambdaQuery(wxInfo);
        if (wxInfo.getUserInfoId() != null){
            lqw.eq(WxInfo::getUserInfoId ,wxInfo.getUserInfoId());
        }
        if (StringUtils.isNotBlank(wxInfo.getWxId())){
            lqw.eq(WxInfo::getWxId ,wxInfo.getWxId());
        }
        if (StringUtils.isNotBlank(wxInfo.getWxh())){
            lqw.eq(WxInfo::getWxh ,wxInfo.getWxh());
        }
        if (StringUtils.isNotBlank(wxInfo.getWxRemark())){
            lqw.eq(WxInfo::getWxRemark ,wxInfo.getWxRemark());
        }
        if (StringUtils.isNotBlank(wxInfo.getWxName())){
            lqw.like(WxInfo::getWxName ,wxInfo.getWxName());
        }
        if (StringUtils.isNotBlank(wxInfo.getToWxId())){
            lqw.eq(WxInfo::getToWxId ,wxInfo.getToWxId());
        }
        if (wxInfo.getRemainingAmount() != null){
            lqw.eq(WxInfo::getRemainingAmount ,wxInfo.getRemainingAmount());
        }
        List<WxInfo> list = iWxInfoService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出租户余额列表
     */
    @PreAuthorize("@ss.hasPermi('wx:wxInfo:export')" )
    @Log(title = "租户余额" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(WxInfo wxInfo) {
        LambdaQueryWrapper<WxInfo> lqw = new LambdaQueryWrapper<WxInfo>(wxInfo);
        List<WxInfo> list = iWxInfoService.list(lqw);
        ExcelUtil<WxInfo> util = new ExcelUtil<WxInfo>(WxInfo. class);
        return util.exportExcel(list, "wxInfo" );
    }

    /**
     * 获取租户余额详细信息
     */
    @PreAuthorize("@ss.hasPermi('wx:wxInfo:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iWxInfoService.getById(id));
    }

    /**
     * 新增租户余额
     */
    @PreAuthorize("@ss.hasPermi('wx:wxInfo:add')" )
    @Log(title = "租户余额" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxInfo wxInfo) {
        return toAjax(iWxInfoService.save(wxInfo) ? 1 : 0);
    }

    /**
     * 修改租户余额
     */
    @PreAuthorize("@ss.hasPermi('wx:wxInfo:edit')" )
    @Log(title = "租户余额" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxInfo wxInfo) {
        return toAjax(iWxInfoService.updateById(wxInfo) ? 1 : 0);
    }

    /**
     * 删除租户余额
     */
    @PreAuthorize("@ss.hasPermi('wx:wxInfo:remove')" )
    @Log(title = "租户余额" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iWxInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
